package CollectionDemo;

import java.util.TreeSet;

public class TreeSetTest {
    public static void main(String[] args) {
        TreeSet<Z> treeSet = new TreeSet<Z>();
        treeSet.add(new Z(5));
        treeSet.add(new Z(-3));
        treeSet.add(new Z(9));
        treeSet.add(new Z(-2));
        System.out.println(treeSet);
        Z first = treeSet.first();
        first.count = 20;
        Z last = treeSet.last();
        last.count = -2;
        System.out.println(treeSet);
    }
}
class Z implements Comparable {

    int count;
    public Z(int count) {
        this.count = count;
    }

    @Override
    public String toString() {
        return "R[count: " + count + "]";
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj != null && Z.class == obj.getClass()) {
            Z z = (Z)obj;
            if (this.count == z.count) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int compareTo(Object o) {
        Z z = (Z)o;
        return this.count > z.count ? 1: this.count < z.count ? -1: 0;
    }
}
